package com.atguigu.bigdata.chapter11.hive;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.catalog.hive.HiveCatalog;

/**
 * @Author lzc
 * @Date 2022/9/13 15:11
 */
public class Flink01_Hive {
    public static void main(String[] args) {
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
    
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
    
    
        // 用的默认的catalog
        tEnv.executeSql("create table person(" +
                            " id string, " +
                            " ts bigint, " +
                            " vc int" +
                            ")with(" +
                            " 'connector' = 'filesystem', " +
                            " 'path' = 'input/sensor.txt', " +
                            " 'format' = 'csv' " +
                            ")");
        
        
        // 1. 先创建一个 HiveCatalog
        // 参数1: catalog的名字 参数2: 连接的时候默认的数据 参数3:hive的配置文件所在的目录
        HiveCatalog hive = new HiveCatalog("hive", "gmall", "input/");
        // 2. 注册 HiveCatalog
        tEnv.registerCatalog("hive", hive);
        tEnv.useCatalog("hive");  // 设置默认catalog
        tEnv.useDatabase("gmall");
        // 3. 通过HiveCatalog来访问hive中表
        // 表的绝对路径: catalog.database.table
//        tEnv.sqlQuery("select * from hive.gmall.person").execute().print();
        tEnv.sqlQuery("select * from person").execute().print();
        
        tEnv.useCatalog("default_catalog");
//        tEnv.sqlQuery("select * from default_catalog.default_database.person").execute().print();
        tEnv.sqlQuery("select * from person").execute().print();
        
        
        
    
    
    }
}
